/*
 * Copyright (c) 2022-2022 All rights Reserved.
 */

package com.driving.core.mapper.handler;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;

import com.driving.core.mapper.type.LicenseType;

/**
 * 数据库驾驶证类型转换器
 *
 * @author SAGITTAR
 * @since 2017
 */
@MappedTypes(value = { LicenseType.class })
@MappedJdbcTypes(value = { JdbcType.VARCHAR })
public class LicenseTypeHandler extends BaseTypeHandler<LicenseType> {

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, LicenseType parameter, JdbcType jdbcType)
            throws SQLException {
        ps.setString(i, parameter.getLicenseCode());
    }

    @Override
    public LicenseType getNullableResult(ResultSet rs, String columnName) throws SQLException {
        return LicenseType.fromType(rs.getString(columnName));
    }

    @Override
    public LicenseType getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        return LicenseType.fromType(rs.getString(columnIndex));
    }

    @Override
    public LicenseType getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        return LicenseType.fromType(cs.getString(columnIndex));
    }
}
